import grammar.Lexer;
import lexical.Analyse;
import midcode.MidcodeGenerator;
import node.CompUnit;
import optimizer.Optimizer;
import symbol.SymbolTable;
import target.TargetCodeGenerator;

import java.util.ArrayList;

public class Compiler {

    public static void main(String[] args) {
        String midcodePath = "midcode.txt";
        String opmidcodePath = "opmidcode.txt";
        String mipsPath = "premips.txt";
        String opmipsPath = "mips.txt";
        ArrayList<String> lines = SysIn.read("testfile.txt");
        Analyse analyse = new Analyse(lines);
        Lexer lexer = new Lexer(analyse.getWords());
        SymbolTable table = new SymbolTable(lexer);
        CompUnit compUnit = new CompUnit(lexer, table);
        SysOut.write("output.txt", compUnit.toString());
        SysOut.write("error.txt", lexer.toStringError());
        // 中间代码
        MidcodeGenerator.Init(compUnit, table);
        SysOut.write(midcodePath, MidcodeGenerator.getInstance().getMidCode());
        // 目标代码
        Optimizer.isOptimise = false;
        TargetCodeGenerator tg = new TargetCodeGenerator();
        SysOut.write(mipsPath, tg.genMipsCode());
        // 优化代码
        Optimizer.isOptimise = true;
        tg = new TargetCodeGenerator();
        SysOut.write(opmipsPath, tg.genMipsCode());
    }
}
